package com.hbwxz.admin.repo;

import com.hbwxz.admin.pojo.CommodityBrand;
import com.hbwxz.admin.pojo.CommodityCategory;
import com.hbwxz.admin.pojo.CommodityCategoryBrandPK;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import javax.persistence.QueryHint;
import java.util.List;
import java.util.Optional;

@Repository
public interface CommodityBrandRepository extends JpaRepository<CommodityBrand, Long> {
    @Override
    @Lock(LockModeType.PESSIMISTIC_READ)
    @QueryHints({@QueryHint(name = "javax.persistence.lock.timeout",value = "3000")})
    Optional<CommodityBrand> findById(Long id);

    CommodityBrand findByName(String name);

    @Query(value = "select b.* from commodity_brand b left join commodity_category_brand cb on b.id=cb.brand_id"+
    "where cb.category_id =: categoryId", nativeQuery = true)
    List<CommodityBrand> listByCategoryId(Long categoryId);

    List<CommodityBrand> findByLetter(String letter);
}
